package group_01.MultiSnakeServer;


import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * 
 * @author Martin Nume
 * ServerListHandler is a thread that returns the list of games to the client.
 */
public class ServerListHandler implements Runnable{

	private Thread thread;
	private WelcomeServer server;
	private Socket clientSocket;
	private PrintWriter out;
	
	public ServerListHandler(WelcomeServer server, Socket socket) {
		this.clientSocket = socket;
		this.server = server;
		out = null;
		thread = new Thread(this);
		thread.start();
	}
	@Override
	public void run() {
		try {
			try {
				//sends the list of servers to the client
				out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); 
			 	out.println(server.getServerList());
			 	out.flush();
			}
			finally {
				//clean up the connections
				if(out != null)
					out.close();
				clientSocket.close();
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
